# -*- coding: utf-8 -*-
'''
   Copyright (c) 2021 FromVB2Python
   FromVB2Python is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
058-选考B-消除字符-202011-16温州选考模拟/58-202011-16-gui .py
请输入由ABCabc字符组成的字符串(直接回车则随机生成):BCcbbaaabAbAaaA
第1次消除的字符：aaa
第2次消除的字符：bbb
第3次消除的字符：AaaA
消除后的字串：BCcAb
'''
import random

def generate():
    global s
    '''生成符串'''
    s = input('请输入由ABCabc字符组成的字符串(直接回车则随机生成):')
    if s=='':
        n = 30
        chrs ='abcABC' 
        s =''.join([random.choice(chrs) for i in range(n)])
        print('原字符串为：{}'.format(s))

def remove():
    '''消除'''
    global s
    i = 0
    m = 1
    while i<len(s)-1:
        k = i
        while isSame(s[i],s[i+1]):
            i += 1
            if i == len(s) -1:
                break

        if i - k >= 2:
            b = s[k:i+1]
            s = s[:k] + s[i+1:]
            # 输出
            print('第{}次消除的字符：{}'.format(m,''.join(b)))
            m += 1            
            i = k - 3
            if i<-1: i=-1
        i += 1
    # 输出结果
    print('消除后的字串：{}'.format(s))

def isSame(a1, a2):
    '''判断2个字符的值是否相等（不区分大小写）'''
    return a1.upper() == a2.upper()

if __name__=='__main__':
    generate()
    remove()